package com.jf.mall.web;

import com.jf.mall.domain.Order;
import com.jf.mall.domain.OrderItem;
import com.jf.mall.domain.ShoppingCart;
import com.jf.mall.domain.User;
import com.jf.mall.service.OrderService;
import com.jf.mall.service.ShoppingCartService;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-10
 * Time:21:57
 */
@WebServlet(name = "OrderServlet", urlPatterns = "/orderServlet")
public class OrderServlet extends BasicServlet {
    private OrderService orderService;
    private ShoppingCartService shoppingCartService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        orderService = new OrderService();
        shoppingCartService = new ShoppingCartService();
    }
    private void order(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect(req.getContextPath() + "/views/user/login.jsp");
            return;
        }
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        if (shoppingCart == null || shoppingCart.isEmpty()) {
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
            return;
        }
        String orderId = orderService.generateOrder(shoppingCart);
        req.setAttribute("orderId", orderId);
        //清空购物车
        shoppingCartService.clearUserShoppingCart(user);
        shoppingCart = new ShoppingCart(user.getId());
        session.setAttribute("shoppingCart", shoppingCart);
        req.getRequestDispatcher("/views/order/checkout.jsp").forward(req, resp);
        //resp.sendRedirect(req.getContextPath() + "/views/order/checkout.jsp");
    }
    private void manage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        }
        List<Order> orders = orderService.getOrders(user);
        req.setAttribute("orders", orders);
        req.getRequestDispatcher("/views/order/order.jsp").forward(req, resp);
    }
    private void details(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        }
        String orderId = req.getParameter("orderId");
        req.setAttribute("orderId", orderId);
        List<OrderItem> orderItems = orderService.getOrderItems(orderId);
        req.setAttribute("orderItems", orderItems);
        int count = 0;
        BigDecimal amount = new BigDecimal(0);
        for (OrderItem orderItem : orderItems) {
            count += orderItem.getCount();
            amount = amount.add(orderItem.getTotalPrice());
        }
        req.setAttribute("totalCount", count);
        req.setAttribute("amount", amount);
        req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req, resp);
    }
}
